angular.module('app').controller('integralDetailsCtrl', ['$scope', 'utils', '$http', '$rootScope', '$q', 'activity', '$state',
    function ($scope, utils, $http, $rootScope, $q, activity, $state) {
        mui('.mui-scroll-wrapper').scroll({
            indicators: false //是否显示滚动条
        });
        $scope.$on('$viewContentLoaded', function (event) {
            var oh = document.documentElement.clientHeight
            var ow = document.documentElement.clientWidth
            /*设置滚动区域高度*/
            $("#scroll1").height((oh + 50 ) + 'px')
            $("#item1mobile").height((oh + 50) + 'px')
            $("#item2mobile").height((oh + 50) + 'px')
            $("#item3mobile").height((oh + 50) + 'px')
            $("#pullrefresh1").height((oh - 40) + 'px')
            $("#pullrefresh2").height((oh - 40) + 'px')
            $("#pullrefresh3").height((oh - 40) + 'px')
            $("#empty").height((oh ) + 'px').width(ow + 'px')
            setTimeout(function () {
                setPullRefresh(mui("#pullrefresh1"), pullfresh1);
                setPullRefresh(mui("#pullrefresh2"), pullfresh2);
                setPullRefresh(mui("#pullrefresh3"), pullfresh3);
            }, 300)
        });
        $scope.$on('$viewContentLoaded', function (event) {
            setTimeout(function () {
                if($rootScope.isapp) {
                    wv.setHeader('积分明细','1','积分规则','7')
                }
            },300)
        });

        function setPullRefresh(element, pull) {
            element.pullRefresh({
                down: {
                    height: 50,
                    callback: pulldownRefresh,
                    auto: true
                },
                up: {
                    height: 50, //可选.默认50.触发上拉加载拖动距离
                    auto: true, //可选,默认false.自动上拉加载一次
                    contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
                    contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
                    callback: pull //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
                }
            })

        }

        var Slider = mui('#integral-slider').slider();
        Slider.gotoItem(0);
        $('#sliderSegmentedControl').addClass('mui-slider-indicator mui-segmented-control mui-segmented-control-inverted mui-segmented-control-negative');

        /**
         * 积分列表
         * @param res
         */
        function getIntegralList(page) {
            $scope.page = page;
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300313"),
                    body: {
                        type: '',
                        userCode: activity.getSoloCode(),//	用户代码，不能为空
                        page: page,
                        rows: '40'

                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.integralList = res.body.rows;
                    $scope.addintegralList = listType(res.body.rows, '1');
                    $scope.reduceintegralList = listType(res.body.rows, '2');
                }
                mui('#pullrefresh1').pullRefresh().endPulldownToRefresh();
                mui('#pullrefresh1').pullRefresh().refresh(true);
                mui('#pullrefresh2').pullRefresh().endPulldownToRefresh();
                mui('#pullrefresh2').pullRefresh().refresh(true);
                mui('#pullrefresh3').pullRefresh().endPulldownToRefresh();
                mui('#pullrefresh3').pullRefresh().refresh(true);
                if (!res.body.rows.length > 0) {
                    mui('#pullrefresh3').pullRefresh().endPullupToRefresh(true)
                    mui('#pullrefresh2').pullRefresh().endPullupToRefresh(true);
                    mui('#pullrefresh1').pullRefresh().endPullupToRefresh(true);
                    mui('#pullrefresh1').pullRefresh().destroy();
                    mui('#pullrefresh2').pullRefresh().destroy();
                    mui('#pullrefresh3').pullRefresh().destroy();
                }
                $(".mui-pull-bottom-pocket").css('margin-bottom','100px')
            })
        }

        function listType(res, type) {
            var typeList = new Array();
            if (res && res.length > 0) {
                for (var i = 0; i < res.length; i++) {
                    var obj = res[i];
                    if (obj.type == type) {
                        typeList.push(obj);
                    }
                }
            }
            return typeList;
        }


        /*下拉刷新*/
        function pulldownRefresh() {
            getIntegralList(1);

        }

        /*上拉加载*/
        function pullfresh1() {
            $scope.page = $scope.page + 1;
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300313"),
                    body: {
                        type: '',
                        userCode: activity.getSoloCode(),//	用户代码，不能为空
                        page: $scope.page,
                        rows: '40'
                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.integralList = $scope.integralList.concat(res.body.rows);
                    $scope.addintegralList = listType($scope.integralList, '1');
                    $scope.reduceintegralList = listType($scope.integralList, '2');
                    if (res.body.rows.length > 0) {
                        mui('#pullrefresh1').pullRefresh().endPullupToRefresh(false)
                    } else {
                        mui('#pullrefresh1').pullRefresh().endPullupToRefresh(true)
                    }

                }
            });
            mui('#pullrefresh1').pullRefresh().endPulldownToRefresh();
        }


        /*上拉加载*/
        function pullfresh2() {
            $scope.page = $scope.page + 1;
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300313"),
                    body: {
                        type: '',
                        userCode: activity.getSoloCode(),//	用户代码，不能为空
                        page: $scope.page,
                        rows: '40'
                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.integralList = $scope.integralList.concat(res.body.rows);
                    $scope.addintegralList = listType($scope.integralList, '1');
                    $scope.reduceintegralList = listType($scope.integralList, '2');
                    if (res.body.rows.length > 0) {
                        mui('#pullrefresh2').pullRefresh().endPullupToRefresh(false)
                    } else {
                        mui('#pullrefresh2').pullRefresh().endPullupToRefresh(true)
                    }

                }
            });
            mui('#pullrefresh2').pullRefresh().endPulldownToRefresh();
        }

        /*上拉加载*/
        function pullfresh3() {
            $scope.page = $scope.page + 1;
            $http.get(utils.integralSys, {
                params: {
                    header: utils.header("300313"),
                    body: {
                        type: '',
                        userCode: activity.getSoloCode(),//	用户代码，不能为空
                        page: $scope.page,
                        rows: '40'
                    }
                }
            }).then(function (res) {
                if (res.header.re_code == "0000") {
                    $scope.integralList = $scope.integralList.concat(res.body.rows);
                    $scope.addintegralList = listType($scope.integralList, '1');
                    $scope.reduceintegralList = listType($scope.integralList, '2');
                    if (res.body.rows.length > 0) {
                        mui('#pullrefresh3').pullRefresh().endPullupToRefresh(false)
                    } else {
                        mui('#pullrefresh3').pullRefresh().endPullupToRefresh(true)
                    }

                }
            });

            mui('#pullrefresh3').pullRefresh().endPulldownToRefresh();
        }


        /**
         * 获取时间数据
         * @param tradeTime
         * @returns {string}
         */
        $scope.integralCreateTime=function(time) {
            var date = new Date(time);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;    //js从0开始取
            var day = date.getDate();
            var hour = date.getHours();
            var minutes = date.getMinutes();
            var second = date.getSeconds();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (second < 10) {
                second = "0" + second;
            }
            return year + '-' + month + '-' + day + '  ' + hour + ':' + minutes + ":" + second;
        };


    }]);